import echarts from "echarts";

function optionBar() {
  // const res = {A: 3, B: 3, C: 2, tab: '监控系统'}

  const resList = [
    { A: 5, B: 1, C: 2, tab: "继电器保护系统" },
    { A: 3, B: 3, C: 2, tab: "监控系统" },
  ];

  let data = {
    xData: ["A", "B", "C", "", "", "", "A", "B", "C"],
    yData: [1, 3, 4, 0, 0, 0, 2, 3, 2],
    // xData: [],
    // yData: [],
  };
  const seriesData = [];
  const allCount = resList.length * 2 - 1;
  const barWidth = (1 / allCount).toFixed(4) * 100 + "%";
  console.log("kks barWidth", barWidth);
  console.log("kks allCount", allCount);
  for (let index = 0; index < allCount; index++) {
    console.log("index kks", index);
    let seriesItem = null;

    if (index % 2 == 0) {
      const item = resList[index / 2];
      console.log("item kks", item);
      seriesItem = getSeriesItem({ title: item.tab, barWidth });
    } else {
      seriesItem = getNullSeriesItem({ barWidth });
    }
    seriesData.push(seriesItem);
  }

  console.log(" kks seriesData", seriesData);

  const colorTB = [
    ["rgba(30, 110, 207, 1)", "rgba(125, 177, 239, 1)"],
    ["rgba(0, 185, 157, 1)", "rgba(126, 232, 223, 1)"],
    ["rgba(166, 75, 210, 1)", "rgba(247, 114, 209, 1)"],
  ];
  const colorBar = [
    ["rgba(30, 110, 207, .9)", "rgba(125, 177, 239, .8)"],
    ["rgba(0, 185, 157, .9)", "rgba(126, 232, 223, .8)"],
    ["rgba(166, 75, 210, .9)", "rgba(247, 114, 209, .8)"],
  ];

  // console.log("xData kks", xData);
  // console.log("seriesData kks", seriesData);
  return {
    toolbox: {
      show: true,
      feature: {
        dataView: { show: true },
        saveAsImage: {
          //excludeComponents :['toolbox'],
          pixelRatio: 5,
        },
      },
    },
    grid: [
      {
        top: 20,
        bottom: 50,
        left: 50,
        right: 30,
      },
      {
        height: 0,
        bottom: 20,
        left: 56,
        right: 35,
      },
    ],
    tooltip: {
      trigger: "axis",
      axisPointer: { type: "none" },
      backgroundColor: "transparent",
      formatter: (obj) => {
        console.log("obj", obj);

        if (obj[0].name) {
          let str = `<div style="padding: 7px 14px; background: #fff; box-shadow: 2px 2px 7px 0px rgba(0, 0, 0, 0.19); border-radius: 10px; border: solid 1px; font-size: 18px">
          <p style="height: 25px; lineHeight: 25px; color: #000000">评分：${obj[0].name}</p>`;

          obj.forEach((item, index) => {
            str += `<p style="height: 25px; lineHeight: 25px; color: #555555">
                 <i style="display: inline-block; width: 16px; height: 16px; border-radius: 8px; background: ${item.color}; vertical-align: middle"></i>
                  评分个数: ${item.value}
                </p>`;
          });
          // obj.forEach((item, index) => {
          //   str += `<p style="height: 25px; lineHeight: 25px; color: #555555">
          //             <i style="display: inline-block; width: 16px; height: 16px; border-radius: 8px; background: ${item.color}; vertical-align: middle"></i>
          //             评分个数: ${item.value}
          //           </p>`;
          // });
          str += "</div>";
          return str;
        } else {
          return "";
        }
      },
    },
    xAxis: [
      {
        type: "category",
        data: data.xData,
        gridIndex: 0,
        // 标签颜色
        axisLabel: {
          color: "#000",
          textStyle: {
            fontSize: 13, //更改坐标轴文字大小
          },
        },
        // 轴线颜色
        axisLine: {
          lineStyle: {
            color: "#e7e7e7",
          },
        },

        zlevel: 2,
      },

      // 辅助柱形图x轴设置
      {
        type: "category",
        gridIndex: 1,
        // 不显示x轴
        axisLine: {
          show: false,
        },
        axisLabel: {
          show: false,
        },

        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        zlevel: 1,
      },
    ],
    yAxis: [
      {
        type: "value",
        gridIndex: 0,
        axisLabel: {
          color: "#333",
        },
        splitLine: {
          lineStyle: {
            type: "dashed",
          },
        },
        axisLine: {
          lineStyle: {
            color: "#ccc",
          },
        },
        axisTick: {
          lineStyle: {
            color: "#ccc",
          },
        },
      },
      {
        type: "value",
        gridIndex: 1,
        axisLabel: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
      },
    ],
    series: [
      {
        data: data.yData,
        type: "bar",
        label: {
          show: true,
          position: "top",
          textStyle: {
            color: "#555",
          },
          formatter: (params) => {
            // console.log('kks params',params);
            if (params.value == 0) {
              return "";
            } else {
              return params.value;
            }
          },
        },
        barWidth: "100%",
        // 柱形图颜色
        itemStyle: {
          // normal: {
          //   color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //     {
          //       offset: 0,
          //       color: colorBar[0],
          //     },
          //     {
          //       offset: 1,
          //       color: colorBar[1],
          //     },
          //   ]),
          // },
          normal: {
            color: (param) => {
              // console.log('kks1 parms',param);
              // 自定义每个柱状图 不同的颜色
              if (param.name == "A") {
                return "#6d93f2";
              } else if (param.name == "B") {
                return "#86d2ae";
              } else {
                return "#687595";
              }
            },
          },
        },
        xAxisIndex: 0,
        yAxisIndex: 0,
      },
      ...seriesData,
    ],
  };
}

/**
 * 获取 普通series
 * @param {*} param0
 * @returns
 */
function getSeriesItem({ title, barWidth }) {
  return {
    data: [
      {
        name: title,
        value: 1,
      },
    ],
    label: {
      show: true,
      position: "inside",
      formatter: "{b}",
      offset: [0, 10],
      textStyle: {
        color: "#777",
        fontSize: 16,
      },
    },
    type: "bar",
    barGap: 0,
    barWidth, //  "33.3%",
    itemStyle: {
      normal: {
        // color: 'rgba(255, 71, 119, .5)',
        color: "#fff",
      },
    },
    xAxisIndex: 1,
    yAxisIndex: 1,
  };
}
/**
 * 获取占位 series
 * @param {*} param0
 * @returns
 */
function getNullSeriesItem({ barWidth }) {
  return {
    data: [
      {
        name: "",
        value: 1,
      },
    ],
    label: {
      show: true,
      position: "inside",
      // formatter: "{0}",
      formatter: () => {
        return "";
      },
      offset: [0, 100],
      textStyle: {
        color: "#000",
        fontSize: 16,
      },
    },
    type: "bar",
    barGap: 0,
    barWidth, // "33.3%",
    itemStyle: {
      normal: {
        // color: 'rgba(240, 215, 95, .5)',
        color: "#fff",
      },
    },
    xAxisIndex: 1,
    yAxisIndex: 1,
  };
}

export default optionBar;

const data = {
  equipmentSysId: "",
  subItemReason: "",
  data: [
    {
      equipmentSysId: "",
      subItemReason: "",
      isRead: 1,
      evaluationTarget: "xxx系统",
      keyItems: "",
      evaluationResult: "A",
      equipmentSys: "",
      keyComponents: "",
      subItemScore: "",
      equipmentType: "监控系统",
      hookUpTotalDefectType: "",
      equipmentSysWeight: "",
      serialVersionUID: 1,
      scoreSum: "0",
      createdTime: "",
      equipmentName: "18F发电机出口8018断路器系统",
      id: 169717,
      enableFlag: "1",
      evaluarionContent: "历年及年度缺陷评估",
      ruleConfigurationType: "",
      libraryVersionId: 364,
      updatedTime: "",
      professionalManagementId: "",
      updatedBy: "",
      subitemWeight: "",
      indicatorToolJson: "",
      sort: 0,
      correctionEvaluationResult: "A",
      subItemProposal: "",
      projectWeight: "",
      revision: "",
      evaluationInitiationId: 373,
      equipmentTypeId: "",
      evaluarionCriterion:
        "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
      equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11",
      createdBy: "",
      indexScore: "",
      indicatorDescription:
        "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
      typeWeight: "",
      categoryWeight: "",
      subItemDeductPoints: "",
      libraryType: 4,
      remarks: "",
      keyComponentsId: "",
    },
    {
      equipmentSysId: "",
      subItemReason: "",
      isRead: 1,
      evaluationTarget: "xxx系统",
      keyItems: "",
      evaluationResult: "C",
      equipmentSys: "",
      keyComponents: "",
      subItemScore: "",
      equipmentType: "监控系统",
      hookUpTotalDefectType: "",
      equipmentSysWeight: "",
      serialVersionUID: 1,
      scoreSum: "0",
      createdTime: "",
      equipmentName: "18F发电机出口8018断路器系统",
      id: 169718,
      enableFlag: "1",
      evaluarionContent: "历年及年度缺陷评估",
      ruleConfigurationType: "",
      libraryVersionId: 364,
      updatedTime: "",
      professionalManagementId: "",
      updatedBy: "",
      subitemWeight: "",
      indicatorToolJson: "",
      sort: 0,
      correctionEvaluationResult: "C",
      subItemProposal: "",
      projectWeight: "",
      revision: "",
      evaluationInitiationId: 373,
      equipmentTypeId: "",
      evaluarionCriterion:
        "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
      equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11",
      createdBy: "",
      indexScore: "",
      indicatorDescription:
        "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
      typeWeight: "",
      categoryWeight: "",
      subItemDeductPoints: "",
      libraryType: 4,
      remarks: "",
      keyComponentsId: "",
    },
    {
      equipmentSysId: "",
      subItemReason: "",
      isRead: 1,
      evaluationTarget: "xxx系统",
      keyItems: "",
      evaluationResult: "B",
      equipmentSys: "",
      keyComponents: "",
      subItemScore: "",
      equipmentType: "监控系统",
      hookUpTotalDefectType: "",
      equipmentSysWeight: "",
      serialVersionUID: 1,
      scoreSum: "0",
      createdTime: "",
      equipmentName: "18F发电机出口8018断路器系统",
      id: 169719,
      enableFlag: "1",
      evaluarionContent: "历年及年度缺陷评估",
      ruleConfigurationType: "",
      libraryVersionId: 364,
      updatedTime: "",
      professionalManagementId: "",
      updatedBy: "",
      subitemWeight: "",
      indicatorToolJson: "",
      sort: 0,
      correctionEvaluationResult: "B",
      subItemProposal: "",
      projectWeight: "",
      revision: "",
      evaluationInitiationId: 373,
      equipmentTypeId: "",
      evaluarionCriterion:
        "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
      equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11",
      createdBy: "",
      indexScore: "",
      indicatorDescription:
        "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
      typeWeight: "",
      categoryWeight: "",
      subItemDeductPoints: "",
      libraryType: 4,
      remarks: "",
      keyComponentsId: "",
    },
    {
      equipmentSysId: "",
      subItemReason: "",
      isRead: 1,
      evaluationTarget: "xxx系统",
      keyItems: "",
      evaluationResult: "C",
      equipmentSys: "",
      keyComponents: "",
      subItemScore: "",
      equipmentType: "监控系统",
      hookUpTotalDefectType: "",
      equipmentSysWeight: "",
      serialVersionUID: 1,
      scoreSum: "0",
      createdTime: "",
      equipmentName: "18F发电机出口8018断路器操作机构",
      id: 169688,
      enableFlag: "1",
      evaluarionContent: "历年及年度缺陷评估",
      ruleConfigurationType: "",
      libraryVersionId: 364,
      updatedTime: "",
      professionalManagementId: "",
      updatedBy: "",
      subitemWeight: "",
      indicatorToolJson: "",
      sort: 0,
      correctionEvaluationResult: "C",
      subItemProposal: "",
      projectWeight: "",
      revision: "",
      evaluationInitiationId: 373,
      equipmentTypeId: "",
      evaluarionCriterion:
        "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
      equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11AE001",
      createdBy: "",
      indexScore: "",
      indicatorDescription:
        "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
      typeWeight: "",
      categoryWeight: "",
      subItemDeductPoints: "",
      libraryType: 4,
      remarks: "",
      keyComponentsId: "",
    },
    {
      equipmentSysId: "",
      subItemReason: "",
      isRead: 1,
      evaluationTarget: "xxx系统",
      keyItems: "",
      evaluationResult: "A",
      equipmentSys: "",
      keyComponents: "",
      subItemScore: "",
      equipmentType: "监控系统",
      hookUpTotalDefectType: "",
      equipmentSysWeight: "",
      serialVersionUID: 1,
      scoreSum: "0",
      createdTime: "",
      equipmentName: "18F发电机出口80181隔离开关操作机构",
      id: 169704,
      enableFlag: "1",
      evaluarionContent: "历年及年度缺陷评估",
      ruleConfigurationType: "",
      libraryVersionId: 364,
      updatedTime: "",
      professionalManagementId: "",
      updatedBy: "",
      subitemWeight: "",
      indicatorToolJson: "",
      sort: 0,
      correctionEvaluationResult: "A",
      subItemProposal: "",
      projectWeight: "",
      revision: "",
      evaluationInitiationId: 373,
      equipmentTypeId: "",
      evaluarionCriterion:
        "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
      equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11AE201",
      createdBy: "",
      indexScore: "",
      indicatorDescription:
        "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
      typeWeight: "",
      categoryWeight: "",
      subItemDeductPoints: "",
      libraryType: 4,
      remarks: "",
      keyComponentsId: "",
    },
  ],
  isRead: 1,
  evaluationTarget: "xxx系统",
  keyItems: "",
  evaluationResult: "A",
  equipmentSys: "",
  keyComponents: "",
  subItemScore: "",
  equipmentType: "监控系统",
  hookUpTotalDefectType: "",
  equipmentSysWeight: "",
  serialVersionUID: 1,
  scoreSum: "0",
  createdTime: "",
  equipmentName: "18F发电机出口8018断路器系统",
  id: 169717,
  enableFlag: "1",
  evaluarionContent: "历年及年度缺陷评估",
  ruleConfigurationType: "",
  libraryVersionId: 364,
  updatedTime: "",
  professionalManagementId: "",
  updatedBy: "",
  subitemWeight: "",
  indicatorToolJson: "",
  sort: 0,
  correctionEvaluationResult: "A",
  subItemProposal: "",
  projectWeight: "",
  revision: "",
  evaluationInitiationId: 373,
  equipmentTypeId: "",
  evaluarionCriterion:
    "1、对缺陷出现的频率、趋势进行评估\n2、是否存在I类或II类缺陷",
  equipmentNameId: "431b92812bb245c6ac526229cb23aae3|02182BAC11",
  createdBy: "",
  indexScore: "",
  indicatorDescription:
    "1、若出现有规律的缺陷，评价为C；\n2、缺陷出现没有规律且频度不高，评价为B；\n3、否则评价为A",
  typeWeight: "",
  categoryWeight: "",
  subItemDeductPoints: "",
  libraryType: 4,
  remarks: "",
  keyComponentsId: "",
  currentUnit: "18F发电机出口8018断路器系统",
  currentReason: "",
  currentProposal: "",
  isShowPopover: false,
};
